import { delay, http, HttpResponse } from 'msw'
import {
  Pagination,
  Pageable,
  Response,
  SystemProduct,
  SystemProductPageRequest,
} from '@/types'

export const systemProductHandlers = [
  http.get('/api/system/product/findPage', async ({ request }) => {
    await delay(300)
    const url = new URL(request.url)
    const params = Object.fromEntries(
      url.searchParams.entries()
    ) as unknown as Pageable & SystemProductPageRequest
    const pageNumber = Number(params.pageNumber)
    const pageSize = Number(params.pageSize)

    // 计算起始索引
    const start = (pageNumber - 1) * pageSize
    // 计算结束索引
    const end = Math.min(start + pageSize, 1000)

    // 生成当前页数据
    const dataSource: SystemProduct[] = []
    for (let i = start; i < end; i++) {
      const item: SystemProduct = {
        id: i.toString(),
        productCode: `产品${i}`,
        productName: `产品${i}`,
        description: `产品${i}`,
        status: 1,
        creatorId: '1',
        creatorName: '测试',
        createTime: '2025-01-01 00:00:00',
        updateTime: '2025-01-01 00:00:00',
        updaterId: '1',
        updaterName: '测试',
      }
      dataSource.push(item)
    }
    const res: Response<Pagination<SystemProduct>> = {
      success: true,
      code: 200,
      data: { pageNumber: 1, pageSize: 10, total: 1000, records: dataSource },
    }
    return HttpResponse.json(res)
  }),
]
